package edu.hit.mylib.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import edu.hit.mylib.pojo.Userinfo;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 处理特殊字段（逻辑删除等）
 */
public interface UserInfoMapper extends BaseMapper<Userinfo> {

    /**
     * 查询所有被逻辑删除的用户
     * @return 逻辑删除的用户列表 类型:List<Userinfo>
     */
    @Select("SELECT id,name,password,contact,version,creat_time,update_time,deleted FROM userinfo WHERE (deleted = '1')")
    List<Userinfo> queryAllByDeleted();

    /**
     * 恢复被逻辑删除的用户———
     * ———已使用乐观锁、日期自动填充
     * @param userinfo
     * @return
     */
    @Update({"<script>",
            "update userinfo",
            "  <set>",
            "    version=version+1,",
            "    deleted=0,",
            "    update_time=CURRENT_TIMESTAMP",
            "  </set>",
            "where id=#{id}",
            "And version = #{version}",
            "</script>"})
    int recoverDeletedById(Userinfo userinfo);
}
